import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.lib.db.DBWritable;


public class DBRow implements Writable, DBWritable {
	public static int numCols;
	private List<String> columns;
	
	public DBRow() {
		this.columns = new ArrayList<String>();
		for (int i = 0; i < numCols; ++i) {
			this.columns.add("default_key_" + i);
		}
	}

	public void readFields(DataInput in) throws IOException {
		for (int i = 0; i < numCols; ++i) {
			this.columns.set(i, Text.readString(in));
		}
	}

	public void readFields(ResultSet resultSet) throws SQLException {
		for (int i = 0; i < numCols; ++i) {
			this.columns.set(i, resultSet.getString(i + 1));
		}
	}

	public void write(DataOutput out) throws IOException {
		for (int i = 0; i < numCols; ++i) {
			Text.writeString(out, this.columns.get(i));
		}
	}

	public void write(PreparedStatement stmt) throws SQLException {
		for (int i = 0; i < numCols; ++i) {
			stmt.setString(i, this.columns.get(i));
		}
	}
	
	public List<CooccurData> getCooccurances() {
		List<CooccurData> cooccurances = new ArrayList<CooccurData>();
		for (int i = 0; i < numCols; ++i) {
			String valueI = i + "_" + this.columns.get(i);
			for (int j = i + 1; j < numCols; ++j) {
				String valueJ = j + "_" + this.columns.get(j);
				String key = valueI + ":" + valueJ;
				CooccurData cooccurance = new CooccurData(key, 1);
				cooccurances.add(cooccurance);
			}
		}
		return cooccurances;
	}
	
	public List<String> getValues() {
		List<String> returnValues = new ArrayList<String>();
		int count = 0;
		for (String column : this.columns) {
			returnValues.add(count + "_" + column);
			++count;
		}
		return returnValues;
	}
}